package com.leo.tool.jdk.jdk8;

import org.junit.Test;

import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class HigherOrderFunctionTest {

    public <T> void producerConsumerInner(Executor executor,
                                          Consumer<Consumer<T>> producer,
                                          Consumer<Supplier<T>> consumer) {
        BlockingQueue<T> blockingQueue = new LinkedBlockingQueue<>();
        executor.execute(() -> producer.accept(blockingQueue::add));
        executor.execute(() -> consumer.accept(() -> {
            try {
                return blockingQueue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }));
    }

    @Test
    public void testProducerConsumerInner() {
        this.<Integer>producerConsumerInner(
                Runnable::run,
                producer -> {
                    producer.accept(1);
                    producer.accept(2);
                },
                consumer -> {
                    assert consumer.get() == 1;
                    assert consumer.get() == 2;
                });
    }
}
